﻿Imports System.IO
Imports MessageBoxEx
Imports System.Collections.Specialized

Public Class frmWizard

    Private _active As Panel
    Private Sub Form_Panel_Show(ByVal activatePanel As Panel)

        Dim _fontRegular As New Font(labelStep_Step1.Font, FontStyle.Regular)
        Dim _fontBold As New Font(labelStep_Step1.Font, FontStyle.Bold)
        Dim _panelName As String = Strings.Right(activatePanel.Name, 5)
        Dim _num As Integer = CInt(Strings.Right(activatePanel.Name, 1))

        _active = activatePanel

        If _active Is _panelStep0 Then

            buttonNext.Text = "&Start"
            buttonBack.Enabled = False

        ElseIf _active Is _panelStep1 Then

            buttonNext.Text = "&Finish"

        Else

            buttonNext.Text = "&Next >"
            buttonBack.Enabled = True
            buttonNext.Enabled = True

        End If

        For Each _panel As Control In Controls

            If TypeOf _panel Is Panel Then
                If (_panel Is activatePanel) Then

                    _panel.Visible = True
                    _panel.BringToFront()

                Else

                    _panel.Visible = False

                End If
            End If

        Next

        For Each _label As Control In Me.Controls

            If TypeOf _label Is Label Then
                If _label.Name.Contains(_panelName) Then
                    _label.Font = _fontBold
                Else
                    _label.Font = _fontRegular
                End If
            End If

        Next

        For Each _pictureBox As Control In Me.Controls

            If TypeOf _pictureBox Is PictureBox Then

                If _pictureBox.Name.Contains("Step") Then

                    Dim _pictNum = CInt(Strings.Right(_pictureBox.Name, 1))
                    Dim _pict As PictureBox = CType(_pictureBox, PictureBox)

                    If _pictNum < _num Then
                        _pict.Image = My.Resources.Resources.icon_wizard_checkmark
                    ElseIf _pictNum = _num Then
                        _pict.Image = My.Resources.Resources.icon_wizard_arrow
                    Else
                        _pict.Image = Nothing
                    End If

                End If

            End If
        Next

    End Sub

    Private Function Folder_ConfirmCreation(ByVal MsgDirectory As String) As Boolean

        If Directory.Exists(MsgDirectory) = True Then Return True

        Dim _Msg As New MsgBoxEx("The directory """ & MsgDirectory & """ does not exist. Would you like us to create it?", Me.Text, , MessageBoxIcon.Question)
        _Msg.SetButtons(New String() {"&Yes", "&No"}, New DialogResult() {DialogResult.Yes, DialogResult.No}, 2)
        _Msg.ShowDialog()

        Dim _MsgResult As DialogBoxResult = _Msg.DialogBoxResult

        If _MsgResult = DialogBoxResult.Button1 Then

            Return True

        End If

        Return False


    End Function

    Private Sub buttonNext_Click(sender As Object, e As EventArgs) Handles buttonNext.Click

        If (_active Is _panelStep0) Then

            Form_Panel_Show(_panelStep1)

        ElseIf (_active Is _panelStep1) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomDemonbuddy.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep2)

            End If

        ElseIf (_active Is _panelStep2) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomMods.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep3)

            End If

        ElseIf (_active Is _panelStep3) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomUtilities.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep4)

            End If

        ElseIf (_active Is _panelStep4) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomItemRules.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep5)

            End If

        ElseIf (_active Is _panelStep5) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomProfiles.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep6)

            End If

        ElseIf (_active Is _panelStep6) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomBackup.Text)

            If _Proceed = True Then

                Form_Panel_Show(_panelStep7)

            End If

        ElseIf (_active Is _panelStep7) Then

            Dim _Proceed As Boolean = Folder_ConfirmCreation(textboxCustomDiablo.Text)

            If _Proceed = True Then

                Close()

            End If

        End If

    End Sub

    Private Function Form_Save() As Boolean

        Dim _demonbuddy As String = textboxCustomMods.Text
        Dim _diablo As String = textboxCustomDiablo.Text
        Dim _itemrule As String = textboxCustomItemRules.Text
        Dim _profile As String = textboxCustomProfiles.Text
        Dim _mod As String = textboxCustomMods.Text
        Dim _backup As String = textboxCustomBackup.Text

        Dim _Msg As New MsgBoxEx("Here are the following settings Demonbuddy Modification Assistant is going to use:" & vbCrLf & vbCrLf & _
                                 "Demonbuddy Directory: " & vbCrLf & _
                                 _demonbuddy & vbCrLf & vbCrLf & _
                                 "Modification Directory: " & vbCrLf & _
                                 _mod & vbCrLf & vbCrLf & _
                                 "Profile Directory: " & vbCrLf & _
                                 _profile & vbCrLf & vbCrLf & _
                                 "Item Rule Directory: " & vbCrLf & _
                                 _itemrule & vbCrLf & vbCrLf & _
                                 "Backup Directory: " & vbCrLf & _
                                 _backup & vbCrLf & vbCrLf & _
                                 "Diablo Directory: " & vbCrLf & _
                                 _diablo & vbCrLf & vbCrLf & _
                                 "Can we proceed?", Me.Text, , MessageBoxIcon.Question)

        _Msg.SetButtons(New String() {"&Yes", "&No", "&Exit"}, New DialogResult() {DialogResult.Yes, DialogResult.No, DialogResult.Abort}, 3)
        _Msg.ShowDialog()

        Dim _MsgResult As DialogBoxResult = _Msg.DialogBoxResult

        If _MsgResult = DialogBoxResult.Button2 Then

            Return False

        ElseIf _MsgResult = DialogBoxResult.Button3 Then

            End

        End If

        '--- Modification Folder

        My.Settings.FolderMods = New StringCollection

        If Directory.Exists(_mod) = False Then
            Directory.CreateDirectory(_mod)
        End If

        My.Settings.FolderMods.Add(Folder_Format(_mod))

        '--- Demonbuddy Folder

        If Directory.Exists(_demonbuddy) = False Then
            Directory.CreateDirectory(_demonbuddy)
        End If

        My.Settings.FolderDemonbuddy = Folder_Format(_demonbuddy)

        '--- Item Rule Folder

        If Directory.Exists(_itemrule) = False Then
            Directory.CreateDirectory(_itemrule)
        End If

        My.Settings.FolderItemRule = Folder_Format(_itemrule)

        '--- Profile Folder

        If Directory.Exists(_profile) = False Then
            Directory.CreateDirectory(_profile)
        End If

        My.Settings.FolderProfile = Folder_Format(_profile)

        '--- Backup Folder

        If Directory.Exists(_backup) = False Then
            Directory.CreateDirectory(_backup)
        End If

        My.Settings.FolderBackup = Folder_Format(_backup)

        '--- Diablo Folder

        My.Settings.FolderDiablo = Folder_Format(_diablo)

        '--- Utilities Folder

        If Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Utilities\")) = False Then
            Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Utilities\"))
        End If

        My.Settings.FolderPatch = Folder_Format(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Utilities\"))

        '--- Save and show main form

        My.Settings.Save()

        Return True

    End Function

    Private Sub frmWizard_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

        If buttonNext.Text <> "&Finish" Then

            Dim _Msg As New MsgBoxEx("You must complete the wizard before continuing.", Me.Text, , MessageBoxIcon.Exclamation)

            _Msg.SetButtons(New String() {"&OK"}, New DialogResult() {DialogResult.OK}, 1)
            _Msg.Show()

            e.Cancel = True

        Else

            If Form_Save() = True Then

                frmMain.Enabled = True
                frmMain._triggerCheckForMods = True
                frmMain.DBMA_AfterWizard()
                Hide()

            End If

        End If

    End Sub

    Private Sub frmWizard_Load(sender As Object, e As EventArgs) Handles Me.Load

        Form_Panel_Show(_panelStep0)

        textboxCustomMods.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Demonbuddy\")
        textboxCustomItemRules.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Item Rules\")
        textboxCustomMods.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Mods\")
        textboxCustomProfiles.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Profiles\")
        textboxCustomBackup.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Backup\")

        Dim _diablo As String =
                My.Computer.Registry.GetValue(
                    "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Diablo III",
                    "DisplayIcon", Nothing)

        If Not (String_IsEmpty(_diablo)) Then

            textboxCustomDiablo.Text = _diablo

        End If


    End Sub

    Private Sub buttonBack_Click(sender As Object, e As EventArgs) Handles buttonBack.Click

        If (_active Is _panelStep1) Then
            Form_Panel_Show(_panelStep0)
        ElseIf (_active Is _panelStep2) Then
            Form_Panel_Show(_panelStep1)
        ElseIf (_active Is _panelStep3) Then
            Form_Panel_Show(_panelStep2)
        ElseIf (_active Is _panelStep4) Then
            Form_Panel_Show(_panelStep3)
        ElseIf (_active Is _panelStep5) Then
            Form_Panel_Show(_panelStep4)
        ElseIf (_active Is _panelStep6) Then
            Form_Panel_Show(_panelStep5)
        ElseIf (_active Is _panelStep7) Then
            Form_Panel_Show(_panelStep6)
        End If

    End Sub

    Private Sub checkbox_CheckedChanged(sender As Object, e As EventArgs) Handles checkboxCustomBackup.CheckedChanged, _
                                                                                    checkboxCustomDemonbuddy.CheckedChanged, _
                                                                                    checkboxCustomDiablo.CheckedChanged, _
                                                                                    checkboxCustomItemRules.CheckedChanged, _
                                                                                    checkboxCustomMods.CheckedChanged, _
                                                                                    checkboxCustomProfiles.CheckedChanged, _
                                                                                    checkboxCustomUtilities.CheckedChanged


        Dim _checkbox As CheckBox = CType(sender, CheckBox)
        Dim _name As String = String_TextAfter(_checkbox.Name, "checkboxCustom")
        Dim _textbox As TextBox = CType(Me.Controls("textboxCustom" & _name), TextBox)
        Dim _button As Button = CType(Me.Controls("buttonBrowse" & _name), Button)

        If _checkbox.Checked = True Then

            _textbox.Enabled = True
            _button.Enabled = True

        Else

            If _name = "Diablo" Then
                _textbox.Text = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Diablo III", "DisplayIcon", Nothing)
            Else
                _textbox.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _name & "\")
            End If

            _textbox.Enabled = False
            _button.Enabled = False

        End If

    End Sub

    Private Sub buttonBrowse(sender As Object, e As EventArgs)

        Dim _textbox As TextBox = CType(sender, TextBox)

        _textbox.Text = Dialog_BrowseDirectory(_textbox.Text)

    End Sub
End Class
